﻿using System.Data;
using System.Web.UI.WebControls;

namespace Util
{
    public static class Bindings
    {
        public static void BindDdl(DataTable dt, System.Web.UI.WebControls.DropDownList ddl, string textField, string valueField)
        {
            ddl.DataSource = dt;
            ddl.DataValueField = valueField;
            ddl.DataTextField = textField;
            ddl.DataBind();

            if (ddl.SelectedItem == null)
                ddl.SelectedIndex = 0;
        }

        public static void BindBl(DataTable dt, System.Web.UI.WebControls.BulletedList bl, string textField, string valueField)
        {
            bl.DataSource = dt;
            bl.DataValueField = valueField;
            bl.DataTextField = textField;
            bl.DataBind();
        }

        public static void BindRb(DataView dv, RadioButtonList rbl, string textField, string valueField)
        {
            rbl.DataSource = dv;
            rbl.DataValueField = valueField;
            rbl.DataTextField = textField;
            rbl.DataBind();
        }

        public static void BindGrid(DataTable dt, GridView grid, string dataKeyFieldName)
        {
            grid.DataSource = dt;
            //grid.DataKeyNames.SetValue(dataKeyFieldName, 0);
            grid.DataBind();
        }

        public static void BindRepeater(DataTable dt, Repeater repeater)
        {
            repeater.DataSource = dt;
            repeater.DataBind();
        }

        public static void BindDataList(DataTable dt, DataList datalist, string dataKeyField)
        {
            datalist.DataSource = dt;
            datalist.DataKeyField = dataKeyField;
            datalist.DataBind();
        }
    }
}